package com.example.provider;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyProvider extends ContentProvider {


    //重写call
    @Override
    public Bundle call(@NonNull String method, @Nullable String arg, @Nullable Bundle extras) {
        if (method.equals("onClick")){
            Log.d("call", "onClick: ");
           return createRemoteViewBundle();
        }
        if (method.equals("getData")) {
            Bundle bundle = new Bundle();
            bundle.putString("response", "App-A这是通过call传递的data + method: "+  method);
            return bundle;
        }
        return null;
    }
    @SuppressLint("RemoteViewLayout")
    private Bundle createRemoteViewBundle() {
        // 创建RemoteViews (使用提供者应用的布局资源)
        RemoteViews views = new RemoteViews(getContext().getPackageName(), R.layout.remote_layout);

        // 设置文本
        //views.setTextViewText(R.id.title, "动态内容");

        // 设置点击事件 (核心部分)
        Intent clickIntent = new Intent();
        clickIntent.setClass(getContext(), SeActivtiy.class);

        // 添加启动标志
        clickIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // 创建PendingIntent (Android 12+需要FLAG_IMMUTABLE)
        PendingIntent pendingIntent = PendingIntent.getActivity(
                getContext(),
                0,
                clickIntent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        views.setOnClickPendingIntent(R.id.action_button, pendingIntent);

        // 放入Bundle返回
        Bundle bundle = new Bundle();
        bundle.putParcelable("remote_views", views);
        return bundle;
    }

    @Override
    public boolean onCreate() {
        return true;  // 必须返回 true，表示初始化成功
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
        return 0;
    }

}
